package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.GetStudyCircleBgResponse;
import com.wephone.Student.bean.LiuxuequanAddCommentResponse;
import com.wephone.Student.bean.LiuxuequanDetailResponse;
import com.wephone.Student.bean.data.CarInfoData;
import com.wephone.Student.bean.data.FlyTogetherInfoData;
import com.wephone.Student.bean.data.HouseInfoData;
import com.wephone.Student.bean.data.LiuxuequanCommentData;
import com.wephone.Student.bean.data.LiuxuequanInfoAndComment;
import com.wephone.Student.bean.data.LiuxuequanInfoData;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.RecruitInfoData;
import com.wephone.Student.bean.data.SecondInfoData;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.bean.data.ZhidaoAskData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.PullToRefreshView;
import com.wephone.Student.util.PullToRefreshView.OnFooterRefreshListener;
import com.wephone.Student.util.PullToRefreshView.OnHeaderRefreshListener;
import com.wephone.Student.util.RoundImageView;

public class LiuXueQuanDetailActivity extends BaseActivity {
	private ListView listView;
	private ListViewAdapter adapter;
	private UserInfoData userInfo;
	private View listHeaderView;
	private ImageView itemImageBg;
	private PullToRefreshView pullToRefreshListView;
	private View curShowPinglunView;
	private LiuxuequanInfoData pinglunInfoData;
	private LiuxuequanCommentData delCommentData;
	
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,LiuxuequanDetailResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					LiuxuequanDetailResponse res = (LiuxuequanDetailResponse)response;
					if(res.getData()!=null && res.getData().size()>0){
						findViewById(R.id.liuxuequan_no_focus_layout).setVisibility(View.GONE);
						if(adapter.pageIdx>=res.getTotalPage() || res.getData().size()<Constants.PAGE_SIZE){
							adapter.hasMore = false;
						}else{
							adapter.pageIdx++;
						}
						adapter.getResults().addAll(res.getData());
						adapter.notifyDataSetChanged();
					}else{
						findViewById(R.id.liuxuequan_no_focus_layout).setVisibility(View.VISIBLE);
					}
				}else{
					findViewById(R.id.liuxuequan_no_focus_layout).setVisibility(View.VISIBLE);
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler3(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,GetStudyCircleBgResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					GetStudyCircleBgResponse res = (GetStudyCircleBgResponse)response;
					if(!TextUtils.isEmpty(res.getData().getBgurl())){
						imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+res.getData().getBgurl(), itemImageBg, options2);
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler5(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					adapter.getResults().clear();
					adapter.hasMore = true;
					adapter.pageIdx = 1;
					adapter.notifyDataSetChanged();
					
					RequestParams params = new RequestParams();
					params.put("uId", userInfo.getuId());
					params.put("Page", "1");
					params.put("PageSize", ""+Constants.PAGE_SIZE);
					params.put("fromuId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_STUDY_CIRCLE_URL, params, getHandler());
					
					//detail bg
					RequestParams params2 = new RequestParams();
			 		params2.put("uId", userInfo.getuId());
					HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_STUDY_CIRCLE_BG_URL, params2, getHandler3());
					
					Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler6(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					adapter.notifyDataSetChanged();
					Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler7(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,LiuxuequanAddCommentResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					LiuxuequanAddCommentResponse res = (LiuxuequanAddCommentResponse)response;
					if(res!=null && res.getData()!=null){
						if(pinglunInfoData!=null){
							if(pinglunInfoData.getComment()==null){
								List<LiuxuequanCommentData> commentDataList = new ArrayList<LiuxuequanCommentData>();
								commentDataList.add(res.getData());
								pinglunInfoData.setComment(commentDataList);
							}else{
								pinglunInfoData.getComment().add(res.getData());
							}
							adapter.notifyDataSetChanged();
							Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
						}
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler8(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,LiuxuequanAddCommentResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
//					LiuxuequanAddCommentResponse res = (LiuxuequanAddCommentResponse)response;
					if(pinglunInfoData!=null){
						pinglunInfoData.getComment().remove(delCommentData);
						adapter.notifyDataSetChanged();
						Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler9(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanDetailActivity.this,LiuxuequanAddCommentResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
//					LiuxuequanAddCommentResponse res = (LiuxuequanAddCommentResponse)response;
					if(pinglunInfoData!=null){
						adapter.getResults().remove(pinglunInfoData);
						adapter.notifyDataSetChanged();
						Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==12 && resultCode==11){
	   	 	adapter.getResults().clear();
			adapter.hasMore = true;
			adapter.pageIdx = 1;
			adapter.notifyDataSetChanged();
			
			RequestParams params = new RequestParams();
			params.put("uId", userInfo.getuId());
			params.put("Page", "1");
			params.put("PageSize", ""+Constants.PAGE_SIZE);
			params.put("fromuId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
			HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_STUDY_CIRCLE_URL, params, getHandler());
			
			//detail bg
			RequestParams params2 = new RequestParams();
	 		params2.put("uId", userInfo.getuId());
			HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_STUDY_CIRCLE_BG_URL, params2, getHandler3());
   	 	}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_liuxuequan_detail);
		
		init();
	}
	
	private void init(){
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.btn_top_right).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(getApplicationContext(),CreateLiuxuequanActivity.class),12);
			}
		});
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			userInfo = (UserInfoData)bundle.getSerializable("userInfo");
//			System.out.println("-----uId-----"+uId);
			if(userInfo!=null && !TextUtils.isEmpty(userInfo.getuId())){
				if(userInfo.getuId().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
					((TextView)findViewById(R.id.top_title_text)).setText("我的山楂圈");
				}else{
					if(userInfo.getuGender()!=null && userInfo.getuGender().equals("1")){
						((TextView)findViewById(R.id.top_title_text)).setText("她的山楂圈");
					}else{
						((TextView)findViewById(R.id.top_title_text)).setText("他的山楂圈");
					}
				}
				listView = (ListView)findViewById(R.id.listview);
				listHeaderView = getLayoutInflater().inflate(R.layout.include_liuxuequan_detail_header, null);
				TextView itemName = (TextView)listHeaderView.findViewById(R.id.item_name);
				itemName.setText(userInfo.getuName());
				if(!TextUtils.isEmpty(userInfo.getuIcon())){
					ImageView itemImage = (ImageView)listHeaderView.findViewById(R.id.item_logo_image);
					String endStr = userInfo.getuIcon().substring(userInfo.getuIcon().lastIndexOf("."));
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userInfo.getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
					itemImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Bundle bundle = new Bundle();
							bundle.putSerializable("userData", userInfo);
							startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
						}
					});
				}
				itemImageBg = (ImageView)listHeaderView.findViewById(R.id.item_detail_bg);
				listView.addHeaderView(listHeaderView);
				adapter = new ListViewAdapter(new ArrayList<LiuxuequanInfoData>());
				listView.setAdapter(adapter);
				listView.setOnItemClickListener(new ListView.OnItemClickListener(){
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
							long arg3) {
						// TODO Auto-generated method stub
//						if(arg2>0){
//							Bundle bundle = new Bundle();
//							bundle.putSerializable("infoData", adapter.getResults().get(arg2-1));
//							startActivity(new Intent(getApplicationContext(),LiuXueQuanPinglunDetailActivity.class).putExtras(bundle));
//						}
						if(curShowPinglunView!=null){
							curShowPinglunView.setVisibility(View.INVISIBLE);
						}
						if(arg2==0){
							return;
						}
						startToDetailPage(adapter.getResults().get(arg2-1));
					}});
//				listView.setOnItemLongClickListener(new ListView.OnItemLongClickListener(){
//					@Override
//					public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
//							int arg2, long arg3) {
//						if(curShowPinglunView!=null){
//							curShowPinglunView.setVisibility(View.INVISIBLE);
//						}
//						if(arg2==0){
//							return true;
//						}
//						if(BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(adapter.getResults().get(arg2-1).getUid())){
//							final LiuxuequanInfoData infoData = adapter.getResults().get(arg2-1);
//							new AlertDialog.Builder(LiuXueQuanDetailActivity.this)
//							.setTitle("提示")
//							.setMessage("您确定要删除该山楂圈信息吗?")
//							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//								@Override
//								public void onClick(DialogInterface dialog, int which) {
//									RequestParams params = new RequestParams();
//									params.put("id", infoData.getId());
//									params.put("uid", infoData.getUid());
//									HttpClient.getInstance().get(Constants.API_DELETE_STUDY_CIRCLE_URL, params, getHandler5());
//								}
//							})
//							.setNegativeButton("取消", null).show();
//							return true;
//						}
//						return true;
//					}});
				
				pullToRefreshListView = (PullToRefreshView)findViewById(R.id.pull_refresh_listview);
				pullToRefreshListView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
					@Override
					public void onHeaderRefresh(PullToRefreshView view) {
						adapter.getResults().clear();
						adapter.hasMore = true;
						adapter.pageIdx = 1;
						
						RequestParams params = new RequestParams();
						params.put("uId", userInfo.getuId());
						params.put("Page", "1");
						params.put("PageSize", ""+Constants.PAGE_SIZE);
						params.put("fromuId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_STUDY_CIRCLE_URL, params, getHandler());
						
						//detail bg
						RequestParams params2 = new RequestParams();
				 		params2.put("uId", userInfo.getuId());
						HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_STUDY_CIRCLE_BG_URL, params2, getHandler3());
					}
				});
				pullToRefreshListView.setOnFooterRefreshListener(new OnFooterRefreshListener() {
					@Override
					public void onFooterRefresh(PullToRefreshView view) {
						if(!adapter.hasMore){
							pullToRefreshListView.onFooterRefreshComplete();
							return;
						}
						RequestParams params = new RequestParams();
						params.put("uId", userInfo.getuId());
						params.put("Page", ""+adapter.pageIdx);
						params.put("PageSize", ""+Constants.PAGE_SIZE);
						params.put("fromuId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_STUDY_CIRCLE_URL, params, getHandler());
					}
				});
				
				RequestParams params = new RequestParams();
				params.put("uId", userInfo.getuId());
				params.put("Page", "1");
				params.put("PageSize", ""+Constants.PAGE_SIZE);
				params.put("fromuId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_STUDY_CIRCLE_URL, params, getHandler());
				
				//detail bg
				RequestParams params2 = new RequestParams();
		 		params2.put("uId", userInfo.getuId());
				HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_STUDY_CIRCLE_BG_URL, params2, getHandler3());
			}
		}
		
	}
	
	private void startToDetailPage(LiuxuequanInfoData infoData){
		if(infoData.getType()!=null){
			if(infoData.getType().equals("1")){
				CarInfoData carInfo = infoData.getOtherData().getCarInfoData();
				carInfo.setuInfo(infoData.getuInfo());
				MutualTransaction2DetailActivity.data = carInfo;
				startActivity(new Intent(getApplicationContext(),MutualTransaction2DetailActivity.class));
			}else if(infoData.getType().equals("2")){
				SecondInfoData secondInfo = infoData.getOtherData().getSecondInfoData();
				secondInfo.setuInfo(infoData.getuInfo());
				MutualTransaction1DetailActivity.data = secondInfo;
				startActivity(new Intent(getApplicationContext(),MutualTransaction1DetailActivity.class));
			}else if(infoData.getType().equals("3")){
				HouseInfoData houseInfo = infoData.getOtherData().getHouseInfoData();
				houseInfo.setuInfo(infoData.getuInfo());
				HouseInfoDetailActivity.data = houseInfo;
				startActivity(new Intent(getApplicationContext(),HouseInfoDetailActivity.class));
			}else if(infoData.getType().equals("4")){
				RecruitInfoData recruitInfo = infoData.getOtherData().getRecruitInfoData();
				recruitInfo.setuInfo(infoData.getuInfo());
				MutualTransaction3DetailActivity.data = recruitInfo;
				startActivity(new Intent(getApplicationContext(),MutualTransaction3DetailActivity.class));
			}else if(infoData.getType().equals("5")){
				ZhidaoAskData askData = infoData.getOtherData().getZhidaoAskData();
				askData.setuInfo(infoData.getuInfo());
				Bundle bundle = new Bundle();
				bundle.putSerializable("askData", askData);
				startActivity(new Intent(getApplicationContext(),ZhidaoAnswerListActivity.class).putExtras(bundle));
			}else if(infoData.getType().equals("6")){
				FlyTogetherInfoData flyInfo = infoData.getOtherData().getFlyTogetherInfoData();
				flyInfo.setuInfo(infoData.getuInfo());
				FlyTogetherDetailActivity.data = flyInfo;
				startActivity(new Intent(getApplicationContext(),FlyTogetherDetailActivity.class));
			}else if(infoData.getType().equals("0")){
				Bundle bundle = new Bundle();
				bundle.putSerializable("infoData", infoData);
				startActivity(new Intent(getApplicationContext(),LiuXueQuanPinglunDetailActivity.class).putExtras(bundle));
			}
		}
	}
	
	private DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.liuxuequan_image_default)
    .showImageOnFail(R.drawable.liuxuequan_image_default)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
//    .displayer(new RoundedBitmapDisplayer(1000))
    .build();
	
	private DisplayImageOptions options2 = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.liuxuequan_user_detail_bg)
    .showImageOnFail(R.drawable.liuxuequan_user_detail_bg)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
//    .displayer(new RoundedBitmapDisplayer(1000))
    .build();
	
	private class ListViewAdapter extends BaseAdapter{
		public int pageIdx = 1;
		public boolean hasMore = true;
		private List<LiuxuequanInfoData> results;
		private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		public ListViewAdapter(List<LiuxuequanInfoData> results) {
			super();
			this.results = results;
		}

		public List<LiuxuequanInfoData> getResults() {
			return results;
		}
		
		@Override
		public int getCount() {
			return results.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView = getLayoutInflater().inflate(R.layout.include_liuxuequan_list_item, null);
			}
			LiuxuequanInfoData data = results.get(position);
			TextView itemName = (TextView)convertView.findViewById(R.id.item_name);
			itemName.setText(data.getuInfo().getuName());
			TextView itemContent = (TextView)convertView.findViewById(R.id.item_content);
			if(!TextUtils.isEmpty(data.getContent())){
				itemContent.setVisibility(View.VISIBLE);
				SpannableStringBuilder style=new SpannableStringBuilder(data.getContent());
				Pattern p = Pattern.compile("\u53d1\u5e03\u4e86\u201c.+\u201d");
				Matcher m = p.matcher(data.getContent());
				if(m.find()){
					style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.font_red)),m.start()+4,m.end()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);              
					itemContent.setText(style);
				}else{
					itemContent.setText(data.getContent());
				}
			}else{
				itemContent.setVisibility(View.GONE);
			}
			TextView tuiguangText = (TextView)convertView.findViewById(R.id.item_tuiguang);
			TextView itemTime = (TextView)convertView.findViewById(R.id.item_create_time);
			itemTime.setText(sdf.format(new Date(Long.parseLong(data.getCtime())*1000)));
			if(data.getIs_rec()!=null && data.getIs_rec().equals("1")){
				tuiguangText.setText("推广");
				convertView.findViewById(R.id.item_tuiguang_text).setVisibility(View.VISIBLE);
				convertView.findViewById(R.id.item_tuiguang_text).setTag(data.getUrl());
				convertView.findViewById(R.id.item_tuiguang_text).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						String url = v.getTag().toString();
						if(!TextUtils.isEmpty(url)){
							Intent intent= new Intent();        
						    intent.setAction("android.intent.action.VIEW");    
						    Uri content_url = Uri.parse(url);   
						    intent.setData(content_url);  
						    startActivity(intent);
						}
					}
				});
			}else{
				if(data.getType()!=null && data.getType().equals("0")){
					convertView.findViewById(R.id.item_tuiguang_text).setVisibility(View.GONE);
				}else{
					convertView.findViewById(R.id.item_tuiguang_text).setVisibility(View.VISIBLE);
					convertView.findViewById(R.id.item_tuiguang_text).setTag(data);
					convertView.findViewById(R.id.item_tuiguang_text).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							LiuxuequanInfoData infoData = (LiuxuequanInfoData)v.getTag();
							startToDetailPage(infoData);
						}
					});
				}
				Gson gson = new Gson();
				if(!TextUtils.isEmpty(data.getuInfo().getuLocDetail())){
					MyLocationInfo locInfo = gson.fromJson(data.getuInfo().getuLocDetail(), MyLocationInfo.class);
					if(locInfo.getType().equals("PlanGoOut")){
						tuiguangText.setText("中国 "+locInfo.getCity());
					}else if(locInfo.getType().equals("Returnees")){
						tuiguangText.setText(locInfo.getLoc());
					}else{
						tuiguangText.setText(locInfo.getCity()+" "+locInfo.getCountry());
					}
				}
			}
			
			RoundImageView itemImage = (RoundImageView)convertView.findViewById(R.id.item_logo_image);
			if(!TextUtils.isEmpty(data.getuInfo().getuIcon())){
				String endStr = data.getuInfo().getuIcon().substring(data.getuInfo().getuIcon().lastIndexOf("."));
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
			}else{
				itemImage.setImageResource(R.drawable.default_user_logo);
			}
			itemImage.setTag(data);
			itemImage.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					LiuxuequanInfoData infoData = (LiuxuequanInfoData)v.getTag();
					Bundle bundle = new Bundle();
					bundle.putSerializable("userData", infoData.getuInfo());
					startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
				}
			});
			itemName.setTag(data);
			itemName.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					LiuxuequanInfoData infoData = (LiuxuequanInfoData)v.getTag();
					Bundle bundle = new Bundle();
					bundle.putSerializable("userData", infoData.getuInfo());
					startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
				}
			});
			ImageView addCommentImage = (ImageView)convertView.findViewById(R.id.add_comment_image);
			final View itemCommentLayout = convertView.findViewById(R.id.liuxuesheng_item_layout);
			addCommentImage.setTag(data);
			addCommentImage.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(itemCommentLayout.getVisibility()==View.VISIBLE){
						itemCommentLayout.setVisibility(View.INVISIBLE);
					}else{
						if(curShowPinglunView!=null){
							curShowPinglunView.setVisibility(View.INVISIBLE);
						}
						LiuxuequanInfoData data = (LiuxuequanInfoData)v.getTag();
						boolean hasZan = false;
						for(UserInfoData subData:data.getSupportName()){
							if(subData.getuId().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
								hasZan = true;
								break;
							}
						}
						ImageView zanImage = (ImageView)itemCommentLayout.findViewById(R.id.image_zan);
						if(hasZan){
							zanImage.setImageResource(R.drawable.btn_liuxuequan_zan_cancel_selected);
						}else{
							zanImage.setImageResource(R.drawable.btn_liuxuequan_zan_selected);
						}
						itemCommentLayout.setVisibility(View.VISIBLE);
						curShowPinglunView = itemCommentLayout;
					}
				}
			});
//			final View currentView = convertView.findViewById(R.id.liuxuesheng_item_layout);
			convertView.findViewById(R.id.image_zan).setTag(data);
			convertView.findViewById(R.id.image_zan).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					itemCommentLayout.setVisibility(View.GONE);
					LiuxuequanInfoData data = (LiuxuequanInfoData)v.getTag();
					if(data.getSupportName()!=null && data.getSupportName().size()>0){
						for(UserInfoData subData:data.getSupportName()){
							if(subData.getuId().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
								data.getSupportName().remove(subData);
								
								RequestParams params = new RequestParams();
								params.put("id", data.getId());
								params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
								params.put("statu", "0");
								HttpClient.getInstance().get(Constants.API_ZHIDAO_ADD_STUDY_CIRCLE_SUPPORT_URL, params, getHandler6());
								return;
							}
						}
					}
					if(data.getSupportName()==null){
						List<UserInfoData> infoDataList = new ArrayList<UserInfoData>();
						infoDataList.add(BaseActivity.getMyInfo(getApplicationContext()));
						data.setSupportName(infoDataList);
					}else{
						data.getSupportName().add(BaseActivity.getMyInfo(getApplicationContext()));
					}
					
					RequestParams params = new RequestParams();
					params.put("id", data.getId());
					params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("statu", "1");
					HttpClient.getInstance().get(Constants.API_ZHIDAO_ADD_STUDY_CIRCLE_SUPPORT_URL, params, getHandler6());
				}
			});
			convertView.findViewById(R.id.image_pinglun).setTag(data);
			convertView.findViewById(R.id.image_pinglun).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					LiuxuequanInfoData data = (LiuxuequanInfoData)v.getTag();
					pinglunInfoData = data;
					showAddCommentDialog("","",data.getId());
				}
			});
			if(data.getuInfo().getuId().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
				convertView.findViewById(R.id.item_delete_liuxuequan).setVisibility(View.VISIBLE);
				convertView.findViewById(R.id.item_delete_liuxuequan).setTag(data);
				convertView.findViewById(R.id.item_delete_liuxuequan).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						final LiuxuequanInfoData infoData = (LiuxuequanInfoData)v.getTag();
						new AlertDialog.Builder(LiuXueQuanDetailActivity.this)
						.setTitle("提示")
						.setMessage("您确定要删除该山楂圈信息吗?")
						.setPositiveButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								pinglunInfoData = infoData;
								
								RequestParams params = new RequestParams();
								params.put("id", infoData.getId());
								params.put("uid", infoData.getUid());
								HttpClient.getInstance().get(Constants.API_DELETE_STUDY_CIRCLE_URL, params, getHandler9());
							}
						})
						.setNegativeButton("取消", null).show();
					}
				});
			}else{
				convertView.findViewById(R.id.item_delete_liuxuequan).setVisibility(View.GONE);
			}
			
			if(data.getImg()!=null){
				if(data.getImg().length>0){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_1);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[0], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_1).setVisibility(View.GONE);
				}
				if(data.getImg().length>1){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_2);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[1], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_2).setVisibility(View.GONE);
				}
				if(data.getImg().length>2){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_3);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[2], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_3).setVisibility(View.GONE);
				}
				if(data.getImg().length>3){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_4);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[3], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_4).setVisibility(View.GONE);
				}
				if(data.getImg().length>4){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_5);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[4], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_5).setVisibility(View.GONE);
				}
				if(data.getImg().length>5){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_6);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[5], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_6).setVisibility(View.GONE);
				}
				if(data.getImg().length>6){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_7);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[6], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_7).setVisibility(View.GONE);
				}
				if(data.getImg().length>7){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_8);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[7], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_8).setVisibility(View.GONE);
				}
				if(data.getImg().length>8){
					ImageView itemAddImage = (ImageView)convertView.findViewById(R.id.item_add_image_9);
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[8], itemAddImage, options);
					itemAddImage.setVisibility(View.VISIBLE);
					itemAddImage.setTag(data);
					itemAddImage.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							LiuxuequanInfoData data = (LiuxuequanInfoData)arg0.getTag();
							
							Bundle bundle  = new Bundle();
							bundle.putSerializable("infoData", data);
							startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtras(bundle));
						}
					});
				}else{
					convertView.findViewById(R.id.item_add_image_9).setVisibility(View.GONE);
				}
			}
			LinearLayout subLayout = (LinearLayout)convertView.findViewById(R.id.zan_and_comment_layout);
			subLayout.removeAllViews();
			//赞
			int subViewIdx = 0;
			if(data.getSupportName()!=null && data.getSupportName().size()>0){
				subViewIdx++;
				View v1 = getLayoutInflater().inflate(R.layout.include_liuxuequan_list_item2, null);
				v1.findViewById(R.id.item_pinglun_layout).setVisibility(View.GONE);
				StringBuffer sb = new StringBuffer();
				for(UserInfoData userInfo:data.getSupportName()){
					sb.append(userInfo.getuName()).append(",");
				}
				sb.deleteCharAt(sb.length()-1);
				((TextView)v1.findViewById(R.id.item_liuxuequan_text_zan)).setText(sb.toString());
				subLayout.addView(v1);
			}
			//评论
			if(data.getComment()!=null && data.getComment().size()>0){
				for(LiuxuequanCommentData comment:data.getComment()){
					View v2 = getLayoutInflater().inflate(R.layout.include_liuxuequan_list_item3, null);
					if(subViewIdx==0){
						subViewIdx++;
						v2 = getLayoutInflater().inflate(R.layout.include_liuxuequan_list_item2, null);
						v2.findViewById(R.id.item_liuxuequan_text_zan).setVisibility(View.GONE);
					}
					if(comment.getReplyuInfo()!=null){
						SpannableStringBuilder style=new SpannableStringBuilder(comment.getuInfo().getuName()+"回复"+comment.getReplyuInfo().getuName()+":");
					    style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.default_gray)),comment.getuInfo().getuName().length(),comment.getuInfo().getuName().length()+2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);              
						((TextView)v2.findViewById(R.id.item_pinglun_name_text)).setText(style);
					}else{
						((TextView)v2.findViewById(R.id.item_pinglun_name_text)).setText(comment.getuInfo().getuName()+":");
					}
					((TextView)v2.findViewById(R.id.item_pinglun_content_text)).setText(comment.getContent());
					LiuxuequanInfoAndComment infoComment = new LiuxuequanInfoAndComment();
					infoComment.setInfoData(data);
					infoComment.setCommentData(comment);
					v2.setTag(infoComment);
					if(BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(comment.getuInfo().getuId()) || BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(data.getUid())){
						v2.setOnLongClickListener(new View.OnLongClickListener() {
							@Override
							public boolean onLongClick(View v) {
								// TODO Auto-generated method stub
								LiuxuequanInfoAndComment infoComment = (LiuxuequanInfoAndComment)v.getTag();
								final LiuxuequanCommentData comment = infoComment.getCommentData();
								final LiuxuequanInfoData infoData = infoComment.getInfoData();
								new AlertDialog.Builder(LiuXueQuanDetailActivity.this)
								.setTitle("提示")
								.setMessage("您确定要删除该回复吗?")
								.setPositiveButton("确定", new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int which) {
										pinglunInfoData = infoData;
										delCommentData = comment;
										
										RequestParams params = new RequestParams();
										params.put("id", comment.getId());
										params.put("uid", comment.getUid());
										HttpClient.getInstance().get(Constants.API_DELETE_STUDY_CIRCLE_REPLY_URL, params, getHandler8());
									}
								})
								.setNegativeButton("取消", null).show();
								return true;
							}
						});
					}else{
						v2.setOnLongClickListener(null);
					}
					v2.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							LiuxuequanInfoAndComment infoComment = (LiuxuequanInfoAndComment)v.getTag();
							LiuxuequanCommentData comment = infoComment.getCommentData();
							pinglunInfoData = infoComment.getInfoData();
							showAddCommentDialog("回复"+comment.getuInfo().getuName(),comment.getId(),comment.getSid());
						}
					});
					subLayout.addView(v2);
				}
			}
			return convertView;
		}		
	}
	
	private void showAddCommentDialog(String dialogTitle,final String replyId,final String sid){
		if(curShowPinglunView!=null){
			curShowPinglunView.setVisibility(View.INVISIBLE);
		}
		final View inputView = getLayoutInflater().inflate(R.layout.dialog_liuxuequan_add_comment_view, null);  
		AlertDialog.Builder builder = new AlertDialog.Builder(LiuXueQuanDetailActivity.this);
		if(!TextUtils.isEmpty(dialogTitle)){
			builder.setTitle(dialogTitle);
		}else{
			builder.setTitle("添加评论");
		}
		builder.setView(inputView);  
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				String content = ((EditText)inputView.findViewById(R.id.dialog_add_comment_input)).getText().toString().trim().replace("\n", "");
				if(content.length()>0){
					RequestParams params = new RequestParams();
					params.put("sid", sid);
					params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("content", content);
					if(!TextUtils.isEmpty(replyId)){
						params.put("reply_id", replyId);
					}
					HttpClient.getInstance().post(Constants.API_ZHIDAO_ADD_STUDY_CIRCLE_REPLY_URL, params, getHandler7());
				}else{
					Toast.makeText(getApplicationContext(), "评论内容不能为空", Toast.LENGTH_SHORT).show();
				}
			}
		});  
		builder.setPositiveButton("取消", null); 
		builder.show();  
	}
	
}
